package common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

public class SendSms {

//    @Resource
//    private static AliSms aliSms;

    private static final String[] SENDTEMPLATE = {
            "SMS_162198586",  //验证码
//            "SMS_158944461",  //接单员进行邮递
//            "SMS_158949152", //审核不通过退回
//            "SMS_158944459",  //进行审核
//            "SMS_158949150",  //指定查新员
//            "SMS_158944457", //发送给全部查新员
//            "SMS_158944454" //新订单给接单员
    };

    public static String generatorCode() {

        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            char ch = str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
//        System.out.println(sb.toString());
        return sb.toString();

    }

    /**
     * 发短信 类型 电话号码 订单号
     *
     * @param phone
     * @return
     */
    public static String sendMsg(String phone) {
        String typetTemplate = "SMS_137880026";
        String code = generatorCode(); //随机验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAILPY1tqnYyyS8", "g51Je2YHUQ1RH3XZ7KZBEpp09btSyb");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "云图书馆");
        request.putQueryParameter("TemplateCode", typetTemplate);
        System.out.println("{\"code\":" + code + "}");
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSON.parseObject(response.getData());
            String reCode = (String) jsonObject.get("Code");
            if (reCode != null && reCode.equals("OK")) {

            } else {
                code = "0000";
            }
        } catch (ServerException e) {
            e.printStackTrace();
            code = "0000";
        } catch (ClientException e) {
            e.printStackTrace();
            code = "0000";
        }
        return code;
    }

    public static void upload(String phone, String unitName) {
        String typetTemplate = "SMS_470145009";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAILPY1tqnYyyS8", "g51Je2YHUQ1RH3XZ7KZBEpp09btSyb");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "云图书馆");
        request.putQueryParameter("TemplateCode", typetTemplate);
        request.putQueryParameter("TemplateParam", "{\"unit_name\":\"" + unitName + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSON.parseObject(response.getData());
            String reCode = (String) jsonObject.get("Code");
            System.out.println("SendMsgResult:" + reCode);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    public static String sendMsgUnit(String phone) {
        String typetTemplate = "SMS_137880026";
        String code = generatorCode(); //随机验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAILPY1tqnYyyS8", "g51Je2YHUQ1RH3XZ7KZBEpp09btSyb");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "云图书馆");
        request.putQueryParameter("TemplateCode", typetTemplate);
        System.out.println("{\"code\":" + code + "}");
        request.putQueryParameter("TemplateParam", "{\"unit_name\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSON.parseObject(response.getData());
            String reCode = (String) jsonObject.get("Code");
            if (reCode != null && reCode.equals("OK")) {

            } else {
                code = "0000";
            }
        } catch (ServerException e) {
            e.printStackTrace();
            code = "0000";
        } catch (ClientException e) {
            e.printStackTrace();
            code = "0000";
        }
        return code;
    }

    public static void uploadFile(String phone, String userName, String fileName, String dataUnique) {
        String typetTemplate = "SMS_469095530";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAILPY1tqnYyyS8", "g51Je2YHUQ1RH3XZ7KZBEpp09btSyb");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "云图书馆");
        request.putQueryParameter("TemplateCode", typetTemplate);

        request.putQueryParameter("TemplateParam", "{\"name\":\"" + userName + "\",\"fileName\":\"" + fileName + "\",\"code\":\"" + dataUnique + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSON.parseObject(response.getData());
            System.out.println(jsonObject);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        uploadFile("13864677931", "济南泉方", "1682737347005.PDF", "26192728");

    }


}

